/**
 * Created by mindUI on 2016/7/19.
 * 操作classJS原声
 */
function hasClass(obj, cls) {
    return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addClass(obj, cls) {
    if (!this.hasClass(obj, cls)) obj.className += " " + cls;
}

function removeClass(obj, cls) {
    if (hasClass(obj, cls)) {
        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
        obj.className = obj.className.replace(reg, ' ');
    }
}

function toggleClass(obj,cls){
    if(hasClass(obj,cls)){
        removeClass(obj, cls);
    }else{
        addClass(obj, cls);
    }
}


function sliderFun(){
    this.container;
    this.sliderObj;
    this.sliderItem;
    this.sliderCount;
    this.insertEl;
    this.insertEllists;
    this.current=0;

}

sliderFun.prototype.init=function(){

    // this.sliderItem[0].style.opacity=1;
    // this.sliderItem[0].style.filter="Alpha(opacity=1)";

    for(var i=0;i<this.sliderCount;i++){
        setOpacity( this.sliderItem[i],0);
        removeClass(this.sliderItem[i], 'active');
    }
    setOpacity(this.sliderItem[0],100);
    addClass( this.sliderItem[0],'active');
	 addClass(document.querySelector('.loading'), 'grid-pages--loaded');
    this.container.style.height=this.sliderItem[0].querySelector('.sliderImg').offsetHeight+'px';
    this.formCircle();

    var obj=this;
    if (this.container.querySelector(".nextButton").addEventListener) {                    //所有主流浏览器，除了 IE 8 及更早 IE版本
        this.container.querySelector(".nextButton").addEventListener("click", function(){
            obj.nextSlider(obj);
        });
    } else if (this.container.querySelector(".nextButton").attachEvent) {                  // IE 8 及更早 IE 版本
        this.container.querySelector(".nextButton").attachEvent("onclick", function(){
            obj.nextSlider(obj);
        });
    }

    if (this.container.querySelector(".prevButton").addEventListener) {                    //所有主流浏览器，除了 IE 8 及更早 IE版本
        this.container.querySelector(".prevButton").addEventListener("click", function(){
            obj.prevSlider(obj);
        });
    } else if (this.container.querySelector(".prevButton").attachEvent) {                  // IE 8 及更早 IE 版本
        this.container.querySelector(".prevButton").attachEvent("onclick", function(){
            obj.prevSlider(obj);
        });
    }


   // setInterval("this.slider(1)",3000);

}

sliderFun.prototype.formCircle=function(){
    var li='';
    var self=this;

    for(var i=0;i<this.sliderCount;i++){
        if(i==this.current){
            li+="<li class='active'></li>";
        }else{
            li+="<li></li>";
        }

    }
    this.insertEl.innerHTML = this.insertEl.innerHTML + li;
    this.insertEllists=this.insertEl.querySelectorAll('li');

    for(var i=0;i<this.insertEllists.length;i++){
        if (this.insertEllists[i].addEventListener) {                    //所有主流浏览器，除了 IE 8 及更早 IE版本
            this.insertEllists[i].addEventListener("click", function(){
                self.changeSlider(this,self);
            });
        } else if (this.insertEllists[i].attachEvent) {                  // IE 8 及更早 IE 版本
            this.insertEllists[i].attachEvent("onclick", function(){
                self.changeSlider(this,self);
            });
        }
    }





}

sliderFun.prototype.changeSlider=function(obj,self){
    for(var k=0;k<self.insertEllists.length;k++){
        if(self.insertEllists[k]==obj){
            self.current=k;
        }
    }

    for(var i=0;i<self.sliderCount;i++){
        // self.sliderItem[i].style.opacity=0;
        // self.sliderItem[i].style.filter="Alpha(opacity=0)";
        setOpacity(self.sliderItem[i],0);

        removeClass(self.sliderItem[i], 'active');
    }
    // self.sliderItem[self.current].style.opacity=1;
    // self.sliderItem[self.current].style.filter="Alpha(opacity=1)";
    setOpacity(self.sliderItem[self.current],100);

    addClass( self.sliderItem[self.current],'active');
    this.reformCircle(self.current);
}

sliderFun.prototype.nextSlider=function(obj){

    //console.log(obj);
    for(var i=0;i<this.sliderCount;i++){
        // this.sliderItem[i].style.opacity=0;
        // this.sliderItem[i].style.filter="Alpha(opacity=0)";

        setOpacity(this.sliderItem[i],0);

        removeClass(this.sliderItem[i], 'active');
    }
//        flat=1表示下一页 flat=0表示上一页

        if(this.current  == (this.sliderCount-1) ){
            this.current=-1;
        }

        var j=parseInt(this.current)+1;


    // this.sliderItem[j].style.opacity=1;
    // this.sliderItem[j].style.filter="Alpha(opacity=1)";\
    setOpacity( this.sliderItem[j],100);
    addClass(this.sliderItem[j],'active');
    this.reformCircle(j);

        ++this.current;

}

sliderFun.prototype.prevSlider=function(){
    this.slider(0);
}
sliderFun.prototype.reformCircle=function(j){
    for(var k=0;k<this.insertEllists.length;k++){
        this.insertEllists[k].className='';
    }


    this.insertEllists[j].className='active';
}
sliderFun.prototype.slider=function(flag){
    for(var i=0;i<this.sliderCount;i++){
        // this.sliderItem[i].style.opacity=0;
        // this.sliderItem[i].style.filter="Alpha(opacity=0)";
        setOpacity( this.sliderItem[i],0);
        removeClass(this.sliderItem[i], 'active');
    }
//        flat=1表示下一页 flat=0表示上一页
    if(flag){
        if(this.current  == (this.sliderCount-1) ){
            this.current=-1;
        }
        var j=this.current+1;
    }else{
        if(this.current  == (0) ){
            this.current=this.sliderCount;
        }
        var j=this.current-1;
    }
    // this.sliderItem[j].style.opacity=1;
    // this.sliderItem[j].style.filter="Alpha(opacity=1)";
    setOpacity(  this.sliderItem[j],100);

    addClass(this.sliderItem[j],'active');
    this.reformCircle(j);
    if(flag){
        ++this.current;
    }else{
        --this.current;
    }
}




function setOpacity(ele, opacity) {

var userAgent = navigator.userAgent,   
rMsie = /(msie\s|trident.*rv:)([\w.]+)/,   
rFirefox = /(firefox)\/([\w.]+)/,   
rOpera = /(opera).+version\/([\w.]+)/,   
rChrome = /(chrome)\/([\w.]+)/,   
rSafari = /version\/([\w.]+).*(safari)/;  
var browser;  
var version;  
var ua = userAgent.toLowerCase();  
 
if(rMsie.exec(ua) != null){  
   if(!opacity){
            ele.style.display="none";
    }else{
            ele.style.display="block";
    }
}else if (rChrome.exec(ua) != null) {  
     var a=rChrome.exec(ua);
     if(a[2].split('.')[0]<=46){
     	if(!opacity){
            ele.style.display="none";
        }else{
        	console.log(opacity);
            ele.style.display="block";
        }
       ele.style.opacity=opacity;
     }else{
     	ele.style.opacity=opacity;
     }
}  else{
	ele.style.opacity=opacity;
}
 


}



// function init(){
//     sliderItem[0].style.opacity=1;
//     addClass( sliderItem[0],'active');
//     container.style.height=sliderItem[0].querySelector('.sliderImg').offsetHeight+'px';
//     formCircle();
//     setInterval("slider(1)",3000);
// }
// //计算下面的圆圈数目
// function formCircle(){
//     var li='';
//     for(var i=0;i<sliderCount;i++){
//         if(i==current){
//             li+="<li class='active' onclick='changeSlider(this)'></li>";
//         }else{
//             li+="<li onclick='changeSlider(this)'></li>";
//         }
//
//     }
//     insertEl.innerHTML = insertEl.innerHTML + li;
//     insertEllists=insertEl.querySelectorAll('li');
//
// }
//
// function changeSlider(obj){
//     for(var k=0;k<insertEllists.length;k++){
//         if(insertEllists[k]==obj){
//             current=k;
//         }
//     }
//
//     for(var i=0;i<sliderCount;i++){
//         sliderItem[i].style.opacity=0;
//         removeClass(sliderItem[i], 'active');
//     }
//     sliderItem[current].style.opacity=1;
//     addClass( sliderItem[current],'active');
//     reformCircle(current);
// }
//
// function nextSlider(){
//     slider(1);
// }
//
// function slider(flag){
//
//     for(var i=0;i<sliderCount;i++){
//         sliderItem[i].style.opacity=0;
//         removeClass(sliderItem[i], 'active');
//     }
// //        flat=1表示下一页 flat=0表示上一页
//     if(flag){
//         if(current  == (sliderCount-1) ){
//             current=-1;
//         }
//         var j=current+1;
//     }else{
//         if(current  == (0) ){
//             current=sliderCount;
//         }
//         var j=current-1;
//     }
//     sliderItem[j].style.opacity=1;
//     addClass( sliderItem[j],'active');
//     reformCircle(j);
//     if(flag){
//         ++current;
//     }else{
//         --current;
//     }
// }
//
//
//
// function reformCircle(j){
//     for(var k=0;k<insertEllists.length;k++){
//         insertEllists[k].className='';
//     }
//     insertEllists[j].className='active';
// }
//
// function prevSlider(){
//     slider(0);
// }